﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Zhaoxi.NET7Demo
{
    /// <summary>
    /// 这个类中  是实现的具体的分表规则
    /// 写入数据的时候, 明确清楚数据写到哪个表中去, 读取数据的时候, 数据需要从哪个表中去读取
    /// </summary>
    public class AreaSubTableService : ISplitTableService
    {

        public static Dictionary<string, string> _AreaDictionary;



        /// <summary>
        /// 整个进程中，执行且只执行一次；
        /// </summary>
        static AreaSubTableService()
        {
            ///根据不同的省份名称，各自分的表，以省份名称的拼音作为后缀；
            _AreaDictionary = new Dictionary<string, string>()
            {
                {"Liaoning","辽宁" },
                {"Heilongjiang","黑龙江" },
                {"Jilin","吉林" },
                {"Hebei","河北" },
                {"Henan","河南" },
                {"Shandong","山东" },
                {"Shaanxi","陕西" },
                {"ShanxiS","山西" },
                {"Jiangsu","江苏" },
                {"Zhejiang","浙江" },
                {"Fujian","福建" },
                {"Guangdong","广东" },
                {"Hainan","海南" },
                {"Yunnan","云南" },
                {"Jiangxi","江西" },
                {"Hunan","湖南" },
                {"Hubei","湖北" },
                {"Sichuan","四川" },
                {"Gansu","甘肃" },
                {"Guizhou","贵州" }
            };
        }



        /// <summary>
        /// 获取所有的表名称
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityInfo"></param>
        /// <param name="tableInfos"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<SplitTableInfo> GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo, List<DbTableInfo> tableInfos)
        {
            return tableInfos
                .Where(c => c.Name.Equals("_Area_"))
                .Select(c => new SplitTableInfo()
                {
                    TableName = c.Name //要用item.name不要写错了
                })
                .OrderBy(it => it.TableName).ToList();
        }

        /// <summary>
        ///  根据在实体中标记的分表依据字段，获取字段的值
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entityInfo"></param>
        /// <param name="splitType"></param>
        /// <param name="entityValue"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue)
        {
            var splitColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute<SplitFieldAttribute>() != null);
            return splitColumn.PropertyInfo.GetValue(entityValue, null);
        }

        /// <summary>
        /// 获取表名称
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo)
        {
            return EntityInfo.DbTableName + "_Area";
        }

        /// <summary>
        /// 获取表名称
        /// </summary>
        /// <param name="db"></param>
        /// <param name="EntityInfo"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public string GetTableName(ISqlSugarClient db, EntityInfo EntityInfo, SplitType type)
        {
            return EntityInfo.DbTableName + "_Area";
        }

        /// <summary>
        /// 获取表名称
        /// </summary>
        /// <param name="db"></param>
        /// <param name="entityInfo"></param>
        /// <param name="splitType"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue)
        { 
            KeyValuePair<string, string> keyPar = _AreaDictionary
             .FirstOrDefault(c => fieldValue.ToString().Contains(c.Value));
            return entityInfo.DbTableName + "_Area_" + keyPar.Key; //根据值按首字母

        }
    }
}
